package com.glzn.springboot03.mapper;

import com.glzn.springboot03.entity.Event;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * @author gongjq
 */
@Mapper
@Repository
public interface EventMapper {
	/**
	 * 查找所有的事件
	 * @param deviceId
	 * @return
	 */
	@Select("SELECT * FROM event WHERE device_id=#{deviceId}")
	List<Event> findAll(Integer deviceId);

	/**
	 * 根据起始和结束事件查询事件
	 * @param startTime
	 * @param endTime
	 * @param deviceId
	 * @return
	 */
	@Select("SELECT * FROM event WHERE happen_time BETWEEN #{startTime} AND subdate(#{endTime},-1) AND device_id=#{deviceId}")
	List<Event> findByTime(Date startTime, Date endTime, Integer deviceId);
	
	/**
	 * 根据起始和结束事件查询事件，然后在过滤制定事件
	 * @param startTime
	 * @param endTime
	 * @param type
	 * @param deviceId
	 * @return
	 */
	@Select("SELECT * FROM event WHERE happen_time BETWEEN #{startTime} AND subdate(#{endTime},-1) AND type=#{type} AND device_id=#{deviceId}")
	List<Event> findByTimeAndType(Date startTime, Date endTime, String type, Integer deviceId);
	/**
	 * 根据事件类型查询事件
	 * @param type
	 * @param deviceId
	 * @return
	 */
	@Select("SELECT * FROM event WHERE type=#{type} AND device_id=#{deviceId}")
	List<Event> findByType(String type, Integer deviceId);

	/**
	 * 插入事件
	 * @param event
	 * @return
	 */
	@SelectKey(statement = "SELECT MAX(id)+1 FROM event", keyProperty = "id", before = true, resultType = int.class)
	@Insert("INSERT INTO event(id,name,type,extend,happen_time,millsecond,device_id) VALUES(#{id},#{name},#{type},#{extend},#{happenTime},#{millsecond},#{deviceId})")
	Boolean insert(Event event);

	/**
	 * 删除全部事件
	 * @param deviceId
	 * @return
	 */
	@Delete("DELETE FROM event WHERE device_id=#{deviceId}")
	boolean deleteAll(Integer deviceId);
}
